//
//  BHCircleDetailReviewInfoCell.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/12.
//

import UIKit

class BHCircleDetailReviewInfoCell: UITableViewCell {
    var deleteSelfReviewBlock:(() -> Void)?
    var reportOtherReviewBlock:(() -> Void)?
    

    @IBOutlet weak var iconImgView: UIButton!
    
    @IBOutlet weak var name_label: UILabel!
    
    @IBOutlet weak var time_label: UILabel!
    
    @IBOutlet weak var content_label: UILabel!
    
    @IBOutlet weak var replyBtn: UIButton!
    
    var model: BHCircleReviewModel! {
        didSet {
            self.name_label.text = model.reviewerName

            self.time_label.text = model.commentTime
            
            self.content_label.attributedText = model.showAttribute
            self.iconImgView.sd_setImage(with: URL.init(string: model.reviewerAvatar), for: .normal, completed: nil)
        }
    }
    
    // 不写没法出现menu
    override var canBecomeFirstResponder: Bool {
        get {
            return true
        }
    }
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if [#selector(removeSelfReviewFunc)].contains(action) {
            return true
        }
        if [#selector(reportOtherAct)].contains(action) {
            return true
        }
        return false
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.iconImgView.addLayerCorner(20, 0, .color_999999)
        
        let longTap = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressCopyAct(_:)))
        longTap.minimumPressDuration = 0.8
        longTap.allowableMovement = 30
        self.addGestureRecognizer(longTap)
    }

    @objc func longPressCopyAct(_ ges: UILongPressGestureRecognizer)  {
        
        if ges.state == .began {
            self.becomeFirstResponder()
            let menu = UIMenuController.shared
            if self.model.reviewerId == BHMeUserInfoModel.shared.userId { // 自己
                let item = UIMenuItem(title: "删除", action: #selector(removeSelfReviewFunc))
                menu.menuItems = [item]
            }else { // 他人
                let item = UIMenuItem(title: "举报", action: #selector(reportOtherAct))
                menu.menuItems = [item]
            }
            
            menu.setTargetRect(ges.view!.frame, in: ges.view!.superview!)
            menu.setMenuVisible(true, animated: true)
            menu.menuItems = nil
        }
    }
    
    @objc func removeSelfReviewFunc() {
        self.deleteSelfReviewBlock?()
    }
    
    @objc func reportOtherAct() {
        self.reportOtherReviewBlock?()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
